package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbRewards;
import com.example.ymm.modules1.mapper.my.user.DbRewardsMapper;
import com.example.ymm.modules1.query.DbRewardsQueryCriteria;
import com.example.ymm.modules1.service.user.DbRewardsService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DbRewardsServiceImpl extends BaseServiceImpl<DbRewardsMapper, DbRewards> implements DbRewardsService {

    @Override
    public TableList<DbRewards> list(DbRewardsQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbRewards> page = new Page<DbRewards>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbRewards.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbRewards> tableList=new TableList(page);
        return tableList;
    }

    public List<DbRewards> queryAll(DbRewardsQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbRewards.class, criteria);
        predicate.orderByDesc("create_time");
        List<DbRewards> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public DbRewards findByName(String name) {
        LambdaQueryWrapper<DbRewards> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DbRewards::getName,name);
        List<DbRewards> dbRewards = baseMapper.selectList(queryWrapper);
        DbRewards rewards=null;
        if(UtilValidate.isNotEmpty(dbRewards)){
            rewards=dbRewards.get(0);
        }
        return rewards;
    }
}
